#include "edge_computing.h"
#include "Logger.h"
#include "utility.h"

struct SockInfo infos[MAX_NUMBER_OF_STATION];

void startup(){
    int is=0;
    communicate1();

    //创建用于记录本次启动运行日期时间的文件
    pthread_mutex_t mutex_lastrun;
    pthread_mutex_lock(&mutex_lastrun);
    std::ofstream fout_lastrun;
    fout_lastrun.open("lastrun.txt", std::ios_base::out | std::ios_base::app);
    if (fout_lastrun.is_open()) {
        time_t t;
        time(&t);
        fout_lastrun << ctime(&t);
        char *tt=NULL;
        tt=ctime(&t);
        fout_lastrun<<tt;
        fout_lastrun.close();
    } else {
        //若失败，记录失败原因和等级
        char *filename=NULL;
        filename="warnings.log";
        string content;
        content="lastrun.txt打开失败\n";
        Logger::instance()->open(filename);
        Logger::instance()->log(Logger::WARN,__FILE__,__LINE__,content);
        Logger::instance()->close();
    }
    pthread_mutex_unlock(&mutex_lastrun);
}

void communicate1(){
    cJSON *one=NULL;
    one=cJSON_CreateObject();
    cJSON_AddNumberToObject(one,"code",1);
    cJSON_AddStringToObject(one,"name","root");
    cJSON_AddStringToObject(one,"password","123456");

    int client_sockfd = 0;
    int len = 0;
    struct sockaddr_in server_addr;
    socklen_t sin_size = 0;
    char *buf= NULL;
    buf=cJSON_Print(one);
    bzero(&server_addr, sizeof(struct sockaddr_in));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr("192.168.1.231");
    server_addr.sin_port = htons(9090);

    if((client_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket error!(ask1)\n");
    }

    sin_size = sizeof(server_addr/*struct sockaddr_in*/);

    if((len = sendto(client_sockfd, buf, strlen(buf), 0 ,(struct sockaddr*)&server_addr, sizeof(struct sockaddr))) < 0)
    {
        perror("sendto fail!(ask1)\n");
    }
    cout<<inet_ntoa(server_addr.sin_addr)<<endl;
    len = recvfrom(client_sockfd,buf , BUFFSIZE, 0, (struct sockaddr*)&server_addr, &sin_size);
    if(len < 0)
    {
        perror("recvfrom error!(ask1)\n");
        char *filename=NULL;
        filename="warnings.log";
        string content;
        content=strerror(errno);

        Logger::instance()->open(filename);
        Logger::instance()->log(Logger::WARN,__FILE__,__LINE__,content);
        Logger::instance()->close();

    }else{
        cJSON *token=0;
        cJSON *two=NULL;
        two= cJSON_Parse(buf);
        token= cJSON_GetObjectItem(two,"token");
        std::string str_token=token->valuestring;
        cout<<str_token<<endl;

        //用文件存储令牌
        ofstream file_token_temp;
        file_token_temp.open("temp_to_token.txt");
        file_token_temp<<str_token<<endl;
        file_token_temp.close();
    }
    close(client_sockfd);

    cJSON_Delete(one);
    //用消息队列发令牌给collect_data

}



//void set(){
//    Timer timer(1000);
//    timer.on_timer([] {
//        std::cout<<"时间到"<<std::endl;
//    });
//    timer.start();
//
//    std::this_thread::sleep_for(std::chrono::seconds(5));
//    timer.stop();
//
//    std::cout << "Timer count: " << timer.get_count() << std::endl;
//}

void collect_data(){

    //preset detached
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

    int server_fd, client_fd;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_addr_len;
    char buf[1024];

    // create a TCP socket
    server_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (server_fd < 0) {
        cerr << "Failed to create socket\n";
        char *filename=NULL;
        filename="errors.log";
        string content;
        content= std::strerror(errno);
        Logger::instance()->open(filename);
        Logger::instance()->log(Logger::WARN,__FILE__,__LINE__,content);
        Logger::instance()->close();
        return ;
    }

    // bind to a specific IP address and port
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr("192.168.1.231"); // bind to a specific IP address
    server_addr.sin_port = htons(33345); // use port in USR-TCP232-304 <Serial Port>
//    client_addr.sin_addr.s_addr = inet_addr("192.168.1.230");
//    client_addr.sin_port = htons(12345);
//    client_addr.sin_addr.s_addr = inet_addr("192.168.1.202");
//    client_addr.sin_port = htons(65300);
    if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        cerr << "Failed to bind to port\n";
        char *filename=NULL;
        filename="errors.log";
        string content= std::strerror(errno);
        Logger::instance()->open(filename);
        Logger::instance()->log(Logger::WARN,__FILE__,__LINE__,content);
        Logger::instance()->close();
        return ;
    }

    // listen for incoming connections
    if (listen(server_fd, 5) < 0) {
        cerr << "Failed to listen for connections\n";
        char *filename=NULL;
        filename="errors.log";
        string content= std::strerror(errno);
        Logger::instance()->open(filename);
        Logger::instance()->log(Logger::WARN,__FILE__,__LINE__,content);
        Logger::instance()->close();
        return ;
    }

    cout << "Waiting for connections...\n";

    client_addr_len= sizeof(client_addr);
    for(int i=0;i<MAX_NUMBER_OF_STATION;i++){
        bzero(&infos[i],sizeof(infos[i]));
        infos[i].fd=-1;
        infos[i].tid=-1;
    }

    client_addr_len= sizeof(client_addr);
    while (1) {

        struct SockInfo* pinfo;
        int tmp_i=0;
        if(tmp_i!=MAX_NUMBER_OF_STATION-1){
            for (int i = 0; i < MAX_NUMBER_OF_STATION; i++) {
                if (infos[i].fd == -1) {
                    pinfo = &infos[i];
                    break;
                }
                if (i == MAX_NUMBER_OF_STATION - 1) {
                    i--;
                }
                tmp_i = i;
            }
        }

        // accept a new connection
        client_fd = accept(server_fd, (struct sockaddr *)&pinfo->addr, &client_addr_len);

        if (client_fd < 0) {
            cerr << "Failed to accept connection\n";
            char *filename=NULL;
            filename="warnings.log";
            string content;
            content= std::strerror(errno);
            Logger::instance()->open(filename);
            Logger::instance()->log(Logger::WARN,__FILE__,__LINE__,content);
            Logger::instance()->close();
            continue;
        }

        //cout << "Accepted connection from " << inet_ntoa(client_addr.sin_addr) << "\n";
        pinfo->fd=client_fd;
        // read data from the client by pthread
        pthread_t pthread_id;
        int ret= pthread_create(&pinfo->tid,&attr,pthcollect,(void*)(long)client_fd);
        pthread_detach(pinfo->tid);                  //新加的，有问题的话删了试试看
        cout << "Closed connection with " << inet_ntoa(client_addr.sin_addr) << "\n";
    }

    close(server_fd);


}

void *pthcollect(void *client_fd){
    char buf[1024];
    pthread_mutex_t mutex_collect;
    int client_fd_=(int)(long)client_fd;
    while(1){
        pthread_mutex_lock(&mutex_collect);
        int n = read(client_fd_, buf, sizeof(buf) - 1);
        if (n < 0) {
            cerr << "Failed to read data from client\n"<<std::strerror(errno)<<endl;
//            char *filename=NULL;
//            filename="errors.log";
//            string content;
//            content= std::strerror(errno);
//            Logger::instance()->open(filename);
//            Logger::instance()->log(Logger::WARN,__FILE__,__LINE__,content);
//            Logger::instance()->close();
        } else if (n == 0) {
            cout << "Client closed the connection\n";
            char *filename;
            filename="warnings.log";
            string content;
            content= std::strerror(errno);
            Logger::instance()->open(filename);
            Logger::instance()->log(Logger::WARN,__FILE__,__LINE__,content);
            Logger::instance()->close();
        } else {
            buf[n] = '\0';

            cout << ") Received data: " << buf << "\n";

//            S
            //do something
            send_data(transer(buf,getSockInfo_addr(client_fd_,MAX_NUMBER_OF_STATION,infos)));

        }
        pthread_mutex_unlock(&mutex_collect);
    }
    close(client_fd_);
}

void send_data(char *buf){
    int server_sockfd = 0;
    int len = 0;
    struct sockaddr_in server_addr;
    int sin_size = 0;
    bzero(&server_addr, sizeof(struct sockaddr_in));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr("192.168.1.231");      //("192.168.141.128");
    server_addr.sin_port = htons(9090);

    if((server_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket error!\n");
        char *filename=NULL;
        filename="errors.log";
        string content;
        content= std::strerror(errno);
        Logger::instance()->open(filename);
        Logger::instance()->log(Logger::WARN,__FILE__,__LINE__,content);
        Logger::instance()->close();
    }

    sin_size = sizeof(struct sockaddr_in);

    if((len = sendto(server_sockfd, buf, strlen(buf), 0 ,(struct sockaddr*)&server_addr, sizeof(struct sockaddr))) < 0)
    {
        perror("sendto fail!\n");
        char *filename=NULL;
        filename="errors.log";
        string content;
        content= std::strerror(errno);
        Logger::instance()->open(filename);
        Logger::instance()->log(Logger::WARN,__FILE__,__LINE__,content);
        Logger::instance()->close();
    }
    close(server_sockfd);

    return;
}

void goodbye(char *token){

    //作废令牌的请求
    cJSON *cjson_ask=NULL;
    cjson_ask=cJSON_CreateObject();
    cJSON_AddNumberToObject(cjson_ask,"code",3);
    char buf_token[16];
    ifstream file_token_temp;
    file_token_temp.open("temp_to_token.txt");
    file_token_temp>>buf_token;
    file_token_temp.close();
    cJSON_AddStringToObject(cjson_ask,"token",buf_token);
    char *buf_ask=NULL;
    buf_ask= cJSON_Print(cjson_ask);
    //发送请求
    int server_sockfd = 0;
    int len = 0;
    struct sockaddr_in server_addr;
    int sin_size = 0;
    bzero(&server_addr, sizeof(struct sockaddr_in));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr("192.168.1.231");      //("192.168.141.128");
    server_addr.sin_port = htons(9090);

    if((server_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket error!\n");
        return ;
    }

    sin_size = sizeof(struct sockaddr_in);

    if((len = sendto(server_sockfd, buf_ask, strlen(buf_ask), 0 ,(struct sockaddr*)&server_addr, sizeof(struct sockaddr))) < 0)
    {
        perror("sendto fail!\n");
        return ;
    }
    close(server_sockfd);

}

void communicate_to_Getter(){
    int server_sockfd = 0;
    int len = 0;
    socklen_t sin_size = 0;
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;
    char buf[BUFFSIZE] = {0};

    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(9091);

    if((server_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket error!\n");
        return ;
    }

    if(bind(server_sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0)
    {
        perror("bind error!\n");
        return ;
    }
    sin_size= sizeof(struct sockaddr_in);
    while(1)
    {
        if((len = recvfrom(server_sockfd, buf, BUFFSIZE, 0, (struct sockaddr*)&client_addr, &sin_size)) < 0)
        {
            perror("recvfrom error!\n");
            return ;
        }

        printf("recv from: %s\n",inet_ntoa(client_addr.sin_addr));
        buf[len] = '\0';
        printf("recv is: %s\n",buf);
        if(len <= 0)
        {
            break;
        }
        cJSON *cjson_buf=NULL;
        cjson_buf= cJSON_Parse(buf);
        cJSON *cjson_actionCode=NULL;
        cjson_actionCode= cJSON_GetObjectItem(cjson_buf,"actionCode");
        switch (cjson_actionCode->valueint) {
            case 1:
            {
                //生成json
                edge_to_getter_2(server_sockfd,client_addr);
            }
                break;
            case 3:
            {
                edge_to_getter_4(server_sockfd,client_addr);
            }
                break;
            case 5:
            {
                edge_to_getter_6(server_sockfd,client_addr);
            }
                break;
            case 7:
            {
                edge_to_getter_8(server_sockfd,client_addr);
            }
                break;
            case 9:
            {
                edge_to_getter_10(server_sockfd,client_addr);
            }
                break;
            case 11:
            {
                edge_to_getter_12(server_sockfd,client_addr);
            }
                break;
            case 13: {
                edge_to_getter_14(server_sockfd, client_addr, cjson_buf);
            }

        }
    }
    close(server_sockfd);
}

void edge_to_getter_2(int server_sockfd,struct sockaddr_in client_addr){
    cJSON *cjson_respone=NULL;
    cjson_respone=cJSON_CreateObject();
    cJSON_AddNumberToObject(cjson_respone,"actionCode",2);
    cJSON_AddNumberToObject(cjson_respone,"success",1);
    cJSON_AddStringToObject(cjson_respone,"os","银河麒麟");
    cJSON_AddStringToObject(cjson_respone,"version","v10");

    ifstream file("lastrun.txt");
    if(!file.is_open()){
        cout<<"Fail to open lastrun.txt"<<endl;
    }
    vector<string> lines;
    string line="正在打开文件...";
    while(getline(file,line)){
        lines.push_back(line);
    }
    if(lines.size()>=2){
        line=lines[lines.size()-2];
    }else{
        line=lines[lines.size()];
    }
    file.close();

    cJSON_AddStringToObject(cjson_respone,"lastRun",line.c_str());
    char *buf_respone=NULL;
    buf_respone= cJSON_Print(cjson_respone);
    cout<<buf_respone<<endl;
    if(sendto(server_sockfd, buf_respone, strlen(buf_respone), 0 ,
              (struct sockaddr*)&client_addr, sizeof(struct sockaddr)) < 0){
        perror("sendto error(communicate to getter code 2)\n");
    }
    cJSON_Delete(cjson_respone);
}

void edge_to_getter_4(int server_sockfd,struct sockaddr_in client_addr){
    cJSON *cjson_respone=NULL;
    cjson_respone=cJSON_CreateObject();
    cJSON_AddNumberToObject(cjson_respone,"actionCode",4);
    cJSON_AddNumberToObject(cjson_respone,"success",1);
    cJSON_AddStringToObject(cjson_respone,"station","1");
    cJSON_AddStringToObject(cjson_respone,"name","station_1");
    char *buf_respone=NULL;
    buf_respone= cJSON_Print(cjson_respone);
    cout<<buf_respone<<endl;
    if(sendto(server_sockfd, buf_respone, strlen(buf_respone), 0 ,
              (struct sockaddr*)&client_addr, sizeof(struct sockaddr)) < 0){
        perror("sendto error(communicate to getter code 6)\n");
    }
    cJSON_Delete(cjson_respone);
}

void edge_to_getter_6(int server_sockfd,struct sockaddr_in client_addr){
    long long e,w,i;
    std::ifstream file1,file2,file3;
    file1.open("errors_count.txt");
    cout<<::strerror(errno)<<endl;
    file2.open("warnings_count.txt");
    cout<<::strerror(errno)<<endl;
    file3.open("infos_count.txt");
    cout<<::strerror(errno)<<endl;
//    if (!file1.is_open()) {
//        std::cerr << "Failed to open the file1: "<< std::endl;
//    }
//    if (!file2.is_open()) {
//        std::cerr << "Failed to open the file2: "<< std::endl;
//    }
//    if (!file3.is_open()) {
//        std::cerr << "Failed to open the file3: "<< std::endl;
//    }
    file1>>e;
    file2>>w;
    file3>>i;
    file1.close();
    file2.close();
    file3.close();
    cJSON *cjson_respone=NULL;
    cjson_respone=cJSON_CreateObject();
    cJSON_AddNumberToObject(cjson_respone,"actionCode",6);
    cJSON_AddNumberToObject(cjson_respone,"success",1);
    cJSON_AddNumberToObject(cjson_respone,"dataCount",1);
    cJSON_AddStringToObject(cjson_respone,"errorCount",to_string(e).c_str());
    cJSON_AddStringToObject(cjson_respone,"warningCount",to_string(w).c_str());
    cJSON_AddStringToObject(cjson_respone,"infoCount",to_string(i).c_str());
    char *buf_respone=NULL;
    buf_respone= cJSON_Print(cjson_respone);
    cout<<buf_respone<<endl;
    if(sendto(server_sockfd, buf_respone, strlen(buf_respone), 0 ,
              (struct sockaddr*)&client_addr, sizeof(struct sockaddr)) < 0){
        perror("sendto error(communicate to getter code 4)\n");
    }
    cJSON_Delete(cjson_respone);
}

void edge_to_getter_8(int server_sockfd,struct sockaddr_in client_addr){
    cJSON *cjson_respone=NULL;
    cjson_respone=cJSON_CreateObject();
    cJSON_AddNumberToObject(cjson_respone,"actionCode",8);
    cJSON_AddNumberToObject(cjson_respone,"success",1);
    cJSON *array=NULL;
    array=cJSON_CreateArray();
    std::ifstream file("errors.log");
    //需要插入日志
    if (!file) {
        std::cerr << "Failed to open the file: "<< std::endl;
    }
//    std::string line;
//    while (std::getline(file, line)) {
//        //std::cout << line << std::endl;
//        cJSON_AddItemToArray(array, cJSON_CreateString(line.c_str()));
//    }
//    file.close();
    std::string line;
    vector<string> lines;
    while (std::getline(file, line)) {
        //std::cout << line << std::endl;
        //cJSON_AddItemToArray(array, cJSON_CreateString(line.c_str()));
        lines.push_back(line);
    }
    file.close();
//    for(long long i=lines.size()-360;i<lines.size();i++){
    for(long long i=0;i<lines.size();i++){
        cJSON_AddItemToArray(array, cJSON_CreateString(lines[i].c_str()));
    }
    lines.clear();
    cJSON_AddItemToObject(cjson_respone,"logError",array);
    char *buf_respone=NULL;
    buf_respone= cJSON_Print(cjson_respone);
    if(sendto(server_sockfd, buf_respone, strlen(buf_respone), 0 ,
              (struct sockaddr*)&client_addr, sizeof(struct sockaddr)) < 0){
        perror("sendto error(communicate to getter code 8)\n");
    }
    cout<<buf_respone<<endl;
    cJSON_Delete(cjson_respone);
}

void edge_to_getter_10(int server_sockfd,struct sockaddr_in client_addr){
    cJSON *cjson_respone=NULL;
    cjson_respone=cJSON_CreateObject();
    cJSON_AddNumberToObject(cjson_respone,"actionCode",10);
    cJSON_AddNumberToObject(cjson_respone,"success",1);
    cJSON *array=NULL;
    array=cJSON_CreateArray();
    std::ifstream file("warnings.log");
    //需要插入日志
    if (!file) {
        std::cerr << "Failed to open the file: "<< std::endl;
    }
//    std::string line;
//    while (std::getline(file, line)) {
//        //std::cout << line << std::endl;
//        cJSON_AddItemToArray(array, cJSON_CreateString(line.c_str()));
//    }
//    file.close();
    std::string line;
    vector<string> lines;
    while (std::getline(file, line)) {
        //std::cout << line << std::endl;
        //cJSON_AddItemToArray(array, cJSON_CreateString(line.c_str()));
        lines.push_back(line);
    }
    file.close();
//    for(long long i=lines.size()-360;i<lines.size();i++){
    for(long long i=0;i<lines.size();i++){
        cJSON_AddItemToArray(array, cJSON_CreateString(lines[i].c_str()));
    }
    lines.clear();
    cJSON_AddItemToObject(cjson_respone,"logWarning",array);
    char *buf_respone=NULL;
    buf_respone= cJSON_Print(cjson_respone);
    if(sendto(server_sockfd, buf_respone, strlen(buf_respone), 0 ,
              (struct sockaddr*)&client_addr, sizeof(struct sockaddr)) < 0){
        perror("sendto error(communicate to getter code 10)\n");
    }
    cout<<buf_respone<<endl;
    cJSON_Delete(cjson_respone);
}

void edge_to_getter_12(int server_sockfd,struct sockaddr_in client_addr){
    cJSON *cjson_respone=NULL;
    cjson_respone=cJSON_CreateObject();
    cJSON_AddNumberToObject(cjson_respone,"actionCode",12);
    cJSON_AddNumberToObject(cjson_respone,"success",1);
    cJSON *array=NULL;
    array=cJSON_CreateArray();
    std::ifstream file("infos.log");
    //需要插入日志
    if (!file) {
        std::cerr << "Failed to open the file: "<< std::endl;
    }
    std::string line;
    vector<string> lines;
    while (std::getline(file, line)) {
        //std::cout << line << std::endl;
        //cJSON_AddItemToArray(array, cJSON_CreateString(line.c_str()));
        lines.push_back(line);
    }
    file.close();
    for(long long i=lines.size()-360;i<lines.size();i++){
        cJSON_AddItemToArray(array, cJSON_CreateString(lines[i].c_str()));
    }
    lines.clear();
    cJSON_AddItemToObject(cjson_respone,"logInfo",array);
    char *buf_respone=NULL;
    buf_respone= cJSON_Print(cjson_respone);
    if(sendto(server_sockfd, buf_respone, strlen(buf_respone), 0 ,
              (struct sockaddr*)&client_addr, sizeof(struct sockaddr)) < 0){
        perror("sendto error(communicate to getter code 12)\n");
    }
    else
        cout<<buf_respone<<endl;
    cJSON_Delete(cjson_respone);
}

void edge_to_getter_14(int server_sockfd,struct sockaddr_in client_addr,cJSON* cjson_buf){
    cJSON *cjson_order=NULL;
    cjson_order= cJSON_GetObjectItem(cjson_buf,"order");
    cJSON *cjson_password=NULL;
    cjson_password= cJSON_GetObjectItem(cjson_buf,"password");
    if(std::strcmp(cjson_password->valuestring,"123456")){
        std::perror("getter密码错误");
        char *buf_respone=NULL;
        cJSON *cjson_response;
        cjson_response=cJSON_CreateObject();
        cJSON_AddNumberToObject(cjson_response,"actionCode",14);
        buf_respone=(char *)"getter密码错误";
        cJSON_AddStringToObject(cjson_response,"orderMessage",buf_respone);
        buf_respone= cJSON_Print(cjson_response);
        if(sendto(server_sockfd, buf_respone, strlen(buf_respone), 0 ,
                  (struct sockaddr*)&client_addr, sizeof(struct sockaddr)) < 0){
            perror("sendto error(communicate to getter code 12)\n");
        }
        cout<<"send:"<<buf_respone<<endl;
    }
    if(!std::strcmp(cjson_order->valuestring,"start")){
        std::cout<<"正在和getter通讯"<<std::endl;
        char *buf_respone=NULL;
        cJSON *cjson_response;
        cjson_response=cJSON_CreateObject();
        cJSON_AddNumberToObject(cjson_response,"actionCode",14);
        buf_respone=(char *)"启动成功";
        cJSON_AddStringToObject(cjson_response,"orderMessage",buf_respone);
        buf_respone= cJSON_Print(cjson_response);
        if(sendto(server_sockfd, buf_respone, strlen(buf_respone), 0 ,
                  (struct sockaddr*)&client_addr, sizeof(struct sockaddr)) < 0){
            perror("sendto error(communicate to getter code 12)\n");
        }
    }else if(!std::strcmp(cjson_order->valuestring,"close")){
        std::cout<<"断开与getter通讯\n";
        char *buf_respone=NULL;
        cJSON *cjson_response;
        cjson_response=cJSON_CreateObject();
        cJSON_AddNumberToObject(cjson_response,"actionCode",14);//
        buf_respone=(char *)"关闭成功";
        cJSON_AddStringToObject(cjson_response,"orderMessage",buf_respone);
        buf_respone= cJSON_Print(cjson_response);
        if(sendto(server_sockfd, buf_respone, strlen(buf_respone), 0 ,
                  (struct sockaddr*)&client_addr, sizeof(struct sockaddr)) < 0){
            perror("sendto error(communicate to getter code 12)\n");
        }
    }
}

char* getIP(int clientSocket){
    struct sockaddr_in clientAddr;
    socklen_t clientAddrlen=sizeof(clientAddr);
    if(getpeername(clientSocket,(struct sockaddr*)&clientAddr,&clientAddrlen)==0){
        char ip[INET_ADDRSTRLEN];
        inet_ntop(AF_INET,&(clientAddr.sin_addr),ip,INET_ADDRSTRLEN);
        return ip;
    }else{
        printf("%s", ::strerror(errno));
        cerr<<"Failed to get client IP information"<<endl;
    }
}
